package com.cmhk.zmail

import grails.transaction.Transactional

@Transactional
class MailSSOService {
    def mailAPIClient
    def oapiClient
    def dingAuthService

    def getMailAddress(String code){
        def accessToken = dingAuthService.getAccessToken()
        def userId = oapiClient.get(path: "/user/getuserinfo", query: [access_token: accessToken,code:code]).json?.userid
        def email = oapiClient.get(path: " /user/get", query: [access_token: accessToken,userid:userId]).json?.email
        return email
    }
    def getSID(String code) {
        println "客户端获取code:${code}"
        def email = getMailAddress(code)
        println "从服务器获取email地址${email}"
        def sid = mailAPIClient.userLogin(email)?.result
        println "从接口获取sid:${sid}"
        return sid
    }
}
